package com.compass.framework.manager

import android.text.TextUtils
import com.compass.framework.constant.CA_SIGN
import com.compass.framework.constant.EXAMINATION_INSPECTION
import com.compass.framework.constant.MEETING_TOKEN
import com.compass.framework.constant.MEETING_UUID
import com.compass.framework.constant.USER_ACCESS_TOKEN
import com.compass.framework.constant.USER_ACC_ID
import com.compass.framework.constant.USER_IM_TOKEN
import com.compass.framework.constant.USER_INFO_DATA
import com.compass.framework.constant.USER_REFRESH_TOKEN
import com.compass.framework.constant.USER_TYPE
import com.compass.framework.ext.toBeanOrNull
import com.compass.framework.model.User
import com.tencent.mmkv.MMKV


/**
 * Date:2023/11/16 17:04
 * @desc   用户信息管理类
 */
object UserInfoManager {

    private var mmkv = MMKV.defaultMMKV()


    /**
     * 是否登录
     * @return Boolean
     */
    fun isLogin(): Boolean {
        return !TextUtils.isEmpty(getImUserAccount())
    }


    /**
     * 保存用户信息
     * @param user
     */
    fun saveUserInfo(user: String?) {
        mmkv.encode(USER_INFO_DATA, user)
    }


    fun saveRefreshToken(token: String?) {
        mmkv.encode(USER_REFRESH_TOKEN, token)
    }

    fun getRefreshToken(): String? {
        return mmkv.decodeString(USER_REFRESH_TOKEN, "")
    }

    fun saveCA(token: String?) {
        mmkv.encode(CA_SIGN, token)
    }

    private fun getCA(): String? {
        return mmkv.decodeString(CA_SIGN, "")
    }

    fun saveMeetingUUID(token: String?) {
        mmkv.encode(MEETING_UUID, token)
    }

    fun getMeetingUUID(): String? {
        return mmkv.decodeString(MEETING_UUID, "")
    }

    fun saveMeetingToken(token: String?) {
        mmkv.encode(MEETING_TOKEN, token)
    }

    fun getMeetingToken(): String? {
        return mmkv.decodeString(MEETING_TOKEN, "")
    }


    fun saveAccessToken(accessToken: String?) {
        mmkv.encode(USER_ACCESS_TOKEN, accessToken)
    }

    fun saveImUserAccount(accId: String?) {
        mmkv.encode(USER_ACC_ID, accId)
    }

    fun getImUserAccount(): String? {
        return mmkv.decodeString(USER_ACC_ID, "")
    }

    fun saveImToken(imToken: String?) {
        mmkv.encode(USER_IM_TOKEN, imToken)
    }

    fun getImUserToken(): String? {
        return mmkv.decodeString(USER_IM_TOKEN, "")
    }


    /**
     * "1 是医生   2医护 3药师
     */
    fun saveUserType(userType: String) {
        mmkv.encode(USER_TYPE, userType)
    }

    fun getUserType(): String? {
        return mmkv.decodeString(USER_TYPE)
    }

    fun getAccessToken(): String? {
        return mmkv.decodeString(USER_ACCESS_TOKEN)
    }


    /**
     * 是否CA签名
     */
    fun isCASignature(): Boolean {
        return true //暂不校验CA
//        return !getCA().isNullOrEmpty()
    }


    /**
     * 是否审核通过
     */
    fun isExamine(): Boolean {
        return getUserInfo()?.auditStatus==1
    }

    /**
     * 获取用户整体信息(医生主页使用)
     */
    fun getUserInfo(): User? {
        val info = mmkv.decodeString(USER_INFO_DATA)
        return info?.toBeanOrNull<User>()

    }


    fun saveExaminationInspectionJson(token: String?) {
        mmkv.encode(EXAMINATION_INSPECTION, token)
    }

     fun getExaminationInspectionJson(): String? {
        return mmkv.decodeString(EXAMINATION_INSPECTION, "")
    }

    /**
     * 清除用户信息、token、imAccId
     */
    fun clearUserInfo() {
        mmkv.encode(USER_INFO_DATA, "")
        saveRefreshToken("")
        saveAccessToken("")
        saveImUserAccount("")
        saveImToken("")
        saveUserType("")
        saveCA("")
        saveExaminationInspectionJson("")

    }


}